package boardgamer.yavalath.client;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;
import boardgamer.common.client.ClickableButton;
import boardgamer.common.client.Drawable;
import boardgamer.common.client.HasEventHandler;
import boardgamer.common.client.SimpleButton;
import boardgamer.common.client.layout.HasLayout;
import boardgamer.common.client.layout.LayoutGroup;
import boardgamer.common.client.layout.TextLayout;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * @author garysoed
 */
public class PlayerButton implements HasLayout, Drawable, HasEventHandler {

  private LayoutGroup layoutGroup;
  private ClickableButton button;
  private TextLayout text;

  private final EventHandler clickHandler;

  public PlayerButton(YavalathPlayer player, HasEventHandler.EventHandler clickHandler) {
    this.clickHandler = checkNotNull(clickHandler, "clickHandler");

    this.button = createButton(player);
    this.text = createText(player, createTextPaint(player));
    this.layoutGroup = new LayoutGroup();
    this.layoutGroup
        .add(button).atCurrentRect()
        .add(text).relativeToEdge(button, LayoutGroup.Direction.RIGHT, 5)
        .verticalAlign(LayoutGroup.VerticalAlign.CENTER);
  }

  public void setPlayer(YavalathPlayer player) {
    ClickableButton newButton = createButton(player);
    newButton.setBoundingRect(this.button.getBoundingRect());
    this.button = newButton;

    Paint textPaint = new Paint(this.text.getPaint());
    textPaint.setColor(RenderHelper.instance().getColor(player.getColor()));
    TextLayout newText = createText(player, textPaint);

    RectF newTextRect = newText.getBoundingRect();
    RectF oldTextRect = this.text.getBoundingRect();
    newTextRect.offsetTo(oldTextRect.left, oldTextRect.top);
    newText.setBoundingRect(newTextRect);
    this.text = newText;

    this.layoutGroup = new LayoutGroup();
    this.layoutGroup
        .add(this.button).atCurrentRect()
        .add(this.text).atCurrentRect();
  }

  private ClickableButton createButton(YavalathPlayer player) {
    Paint buttonTextPaint = createTextPaint(player);
    buttonTextPaint.setColor(Color.BLACK);

    int color = RenderHelper.instance().getColor(player.getColor());
    String playerName = player.getName();
    LetterHex buttonLetter
        = new LetterHex(playerName.charAt(0), color, new PointF(),
        buttonTextPaint, true);

    return new ClickableButton(
        new SimpleButton(buttonLetter, clickHandler, buttonLetter.getBoundingRect()),
        GlowHex.verticalHex(buttonLetter, color));
  }

  private TextLayout createText(YavalathPlayer player, Paint paint) {
    return new TextLayout(player.getName().substring(1), paint);
  }

  private Paint createTextPaint(YavalathPlayer player) {
    Paint textPaint = RenderHelper.instance().getHexLetterPaint();
    textPaint.setColor(RenderHelper.instance().getColor(player.getColor()));
    return textPaint;
  }

  @Override
  public void draw(Canvas canvas) {
    this.button.draw(canvas);
    this.text.draw(canvas);
  }

  @Override
  public void setBoundingRect(RectF boundingRect) {
    layoutGroup.setBoundingRect(boundingRect);
  }

  @Override
  public RectF getBoundingRect() {
    return new RectF(layoutGroup.getBoundingRect());
  }

  @Override
  public void handleEvent(float eventX, float eventY, int eventAction) {
    button.handleEvent(eventX, eventY, eventAction);
  }

  @Override
  public RectF getCollisionBox() {
    return button.getCollisionBox();
  }
}
